package cn.daniel.framework.gateway.filter;
/*

import cn.daniel.framework.core.constants.CommonConstants;
import cn.daniel.framework.core.enums.ApiResultCode;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.setting.dialect.Props;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

*/
/**
 * @author daniel.tuen
 * @date 2025/9/1 14:48
 * @description 白名单
 *//*

@Slf4j
// @Component
public class IpWhiteAddressInterceptor implements HandlerInterceptor {

    */
/**
     * ip white list 配置文件名
     *//*

    private static final String FILE_NAME = "ipwhitelist.properties";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        String appId = request.getHeader(CommonConstants.APP_ID);
        if (StringUtils.isBlank(appId)) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_APP_ID.getMessage());
            return false;
        }

        Props props = new Props(FILE_NAME);
        String ipWhiteList = props.getProperty(appId);
        if (StringUtils.isBlank(ipWhiteList)) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.IP_WHITE_LIST_FAIL.getMessage());
            return false;
        }
        String[] split = ipWhiteList.split(",");
        Set<String> ipWhiteSet = Arrays.stream(split).collect(Collectors.toSet());
        // 获取request 真实的ip 地址
        String httpIp = ServletUtil.getClientIP(request, null);
        log.info("ip 白名单处理:{}", httpIp);
        if (!StringUtils.isEmpty(httpIp)) {
            String[] httpIps = httpIp.split(",");
            Set<String> tempIp = Arrays.stream(httpIps).collect(Collectors.toSet());
            // 存在即为白名单ip
            for (String ip : tempIp) {
                // TODO
                if (ipWhiteSet.contains(ip)) {
                    return true;
                }
            }
        }
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.IP_WHITE_LIST_FAIL.getMessage());
        return false;
    }
}
*/
